import {
    queryData,
    querySecret,
    refreshSecret,
    updateData,
  } from './service';
  
  import { message } from 'antd';
  
  const Model = {
    namespace: 'enterpriseModel',    /*定义model的名称,表示在全局 state 上的 key不能重复 */
    /* model的状态属性 */
    state: {
      enterInfo:{},
      secretInfo:{}
    },
    /*定义model请求service事件,对外的事件 */
    effects: {
      *submit({payload},{call,put}){
        const reps = yield call(updateData,payload);
        const success = reps.success;
        if (success) {
          yield put({
            type: 'get',
            id:payload.id
          });
        }
      },
      *get({ id }, { call,put }) {      
        const reps = yield call(queryData, id);
        const success = reps.success;
        if (success) {
          yield put({
            type: 'getSecret',
            id:id
          });

          yield put({
            type: 'reducerState',
            payload: reps,
          });
        } else {
          message.warning(reps.errorMessage)
        }
      },
      *getSecret({ id }, { call,put }) {      
        const reps = yield call(querySecret, id);
        const success = reps.success;
        if (success) {
          yield put({
            type: 'reducerStateSecret',
            secretInfo: reps.data,
          });
        } else {
          message.warning(reps.errorMessage)
        }
      },
      *refresh({eid,id,field},{call,put}){
        const reps = yield call(refreshSecret, id,field);
        const success = reps.success;
        if (success) {
          yield put({
            type: 'getSecret',
            id:eid
          });

        }
      },
    },
    /*用于修改state状态的事件,或者合并state */
    reducers: {
      reducerState(state, { payload}) {
        /*这是一种特殊的语法，给state属性赋值 */
        return { ...state,  enterInfo:payload.data};
      },
      reducerStateSecret(state, { secretInfo}) {
        /*这是一种特殊的语法，给state属性赋值 */
        return { ...state,  secretInfo:secretInfo};
      },
    },
  };
  export default Model;
  